package ArrayList;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: dch
 * @Date: 2020/6/9 9:12
 * @Description:
 */
public class CollectionThread implements Runnable {
    private static CopyOnWriteArrayList<String> copyOnWriteArrayList = new CopyOnWriteArrayList<>();

    static {
        copyOnWriteArrayList.add("tutu");
        copyOnWriteArrayList.add("daidai");
        copyOnWriteArrayList.add("jiaojiao");
    }
    @Override
    public void run() {

        for(String value:copyOnWriteArrayList){
            //读操作
            System.out.println("读取数据："+value);
            boolean writeflag = copyOnWriteArrayList.add("xxx");
            if(writeflag){
                System.out.println("写入数据：xxx");
            }
        }
    }

    public static void main(String[] args) {
        //创建线程任务
        CollectionThread ct = new CollectionThread();

        //开启10条线程
        for(int i=1;i<=10;i++){
            new Thread(ct).start();
        }
    }
}
